package com.example.animations.controls;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.NumberPicker;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.animations.R;

public class PickerView extends LinearLayout implements NumberPicker.OnValueChangeListener {
    private int _minValue;
    private int _maxValue;
    private int _value;
    private String _title;
    private String[] _stringValues;
    private IOnPickerValueChanged _onPickerValueChanged;
    private NumberPicker _picker;
    private boolean _isEnabled = true;

    public PickerView(Context context) {
        super(context);
        initialize();
    }

    public PickerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public PickerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize();
    }

    private void initialize() {
        inflate(getContext(), R.layout.view_picker, this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        TextView titleTextView = findViewById(R.id.title_view);
        if (_title != null && _title != "") {
            titleTextView.setText(_title);
            titleTextView.setVisibility(VISIBLE);
        }
        else {
            titleTextView.setVisibility(GONE);
        }

        _picker = findViewById(R.id.picker);
        if (_stringValues != null && _stringValues.length > 0) {
            _picker.setDisplayedValues(_stringValues);
        }
        _picker.setMinValue(_minValue);
        _picker.setMaxValue(_maxValue);
        _picker.setValue(_value);
        _picker.setOnValueChangedListener(this);
        _picker.setWrapSelectorWheel(false);
        _picker.setEnabled(_isEnabled);
    }

    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int i1) {
        _value = i1;
        if (_onPickerValueChanged != null) {
            _onPickerValueChanged.OnValueChanged(i1);
        }
    }

    public void setMinMaxValues(int min, int max) {
        _minValue = min;
        _maxValue = max;
    }

    public void setStringValues(String... values) {
        _stringValues = values;
        setMinMaxValues(0, _stringValues.length - 1);
    }

    public void setValue(int value) {
        _value = value;
        if (_picker != null) {
            _picker.setValue(_value);
        }
    }

    public int getValue() {
        return _value;
    }

    public void setTitle(String title) {
        _title = title;
    }

    public void setOnValueChangedListener(IOnPickerValueChanged onPickerValueChanged) {
        _onPickerValueChanged = onPickerValueChanged;
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);
        _isEnabled = enabled;
        if (_picker != null) {
            _picker.setEnabled(enabled);
        }
    }

    public interface IOnPickerValueChanged {
        void OnValueChanged(int value);
    }
}
